package et.six.eta.View;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVCloudQueryResult;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.CloudQueryCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.LogInCallback;

import java.util.Arrays;
import java.util.List;

import et.six.eta.MainActivity;
import et.six.eta.R;
import et.six.eta.Util.app.BaseActivity;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    public static String static_name;
    public static String static_pwd;
    private TextView login;
    private View inputlayout;
    private EditText input_name,input_pwd;
    private float mwidth,mheight;
    private View progress;
    private TextView sign;
    private Handler han=new Handler();
    private  Runnable r=new Runnable() {
        @Override
        public void run() {
//            //计算空间的宽和高
//            mheight=login.getMeasuredHeight();
//            mwidth=login.getMeasuredWidth();
//            //隐藏输入框
//            input_name.setVisibility(View.VISIBLE);
//            input_pwd.setVisibility(View.VISIBLE);
//
//            inputAnimator(inputlayout,mwidth,mheight);
            Intent it=new Intent(act, MainActivity.class);
            it.putExtra("name",input_name.getText().toString());
            it.putExtra("pwd",input_pwd.getText().toString());

            startActivity(it);
            finish();

        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
     login=findViewById(R.id.login_bt);
     inputlayout=findViewById(R.id.login_input_layout);
     input_name=findViewById(R.id.input_layout_name);
     input_pwd=findViewById(R.id.input_layout_pwd);
     progress=findViewById(R.id.login_progress);
     sign=findViewById(R.id.sigup);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
          login.setOnClickListener(this);
          sign.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {



        switch (view.getId()) {
//       Toast.makeText( LoginActivity.this,input_pwd.getText().toString(),Toast.LENGTH_SHORT).show();

            case R.id.login_bt:

                String name = input_name.getText().toString();
                String pwd = input_pwd.getText().toString();
                static_name = name;
                static_pwd = pwd;
                String cql = "select * from signinfo where name=? and pwd=?";

                AVUser.logInInBackground(name, pwd, new LogInCallback<AVUser>() {
                    @Override
                    public void done(AVUser avUser, AVException e) {
                        if (e==null) {
                            //计算空间的宽和高
                            mheight = login.getMeasuredHeight();
                            mwidth = login.getMeasuredWidth();
                            //隐藏输入
                            input_name.setVisibility(View.VISIBLE);
                            input_pwd.setVisibility(View.VISIBLE);
                            login.setVisibility(View.GONE);
                            inputAnimator(inputlayout, mwidth, mheight);
                            han.postDelayed(r, 3000);


                        } else {
                            reconvery();
                            Toast.makeText(act, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }
                });



               /* AVQuery.doCloudQueryInBackground(cql, new CloudQueryCallback<AVCloudQueryResult>() {
                    @Override
                    public void done(AVCloudQueryResult avCloudQueryResult, AVException e) {
                        if (avCloudQueryResult == null) {
                            //han.post(r);
                            //计算空间的宽和高
                            mheight = login.getMeasuredHeight();
                            mwidth = login.getMeasuredWidth();
                            //隐藏输入
                            input_name.setVisibility(View.VISIBLE);
                            input_pwd.setVisibility(View.VISIBLE);
                            login.setVisibility(View.GONE);
                            inputAnimator(inputlayout, mwidth, mheight);

                            han.postDelayed(r, 3000);


                        } else {
                            //han.removeCallbacks(r);
                            reconvery();
                            Toast.makeText(act, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }*/




              break;

            case R.id.sigup:
                startActivity(new Intent(act,SignInActivity.class));
                finish();
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        han.removeCallbacksAndMessages(null);
    }

    private void inputAnimator(final View view, float w, float h){
        AnimatorSet as=new AnimatorSet();
        final ValueAnimator animator=ValueAnimator.ofFloat(0,w);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float values= (float) valueAnimator.getAnimatedValue();
                //ViewGroup.MarginLayoutParams  用于每个子控件的边缘布局信息
                ViewGroup.MarginLayoutParams params= (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                params.leftMargin= (int) values;
                params.rightMargin= (int) values;
                view.setLayoutParams(params);
            }
        });


        ObjectAnimator animator1=ObjectAnimator.ofFloat(inputlayout,"scaleX",1f,0);
        as.setDuration(1000);
        as.setInterpolator(new AccelerateDecelerateInterpolator());
        as.playTogether(animator,animator1);
        as.start();
        as.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                /**
                 * 动画结束后，先显示加载的动画，然后再隐藏输入框
                 */
              progress.setVisibility(View.VISIBLE);
              progressAnimators(progress);
              inputlayout.setVisibility(View.GONE);
            }
        });
    }
      /*
      * 设置进度动画*/
    private void progressAnimators(final  View view){
        // PropertyValuesHolder  属性存储器，为两个执行类提供更新多个属性的功能
        PropertyValuesHolder valuesHolder=PropertyValuesHolder.ofFloat("scaleX",
                0.5f,1f);
        PropertyValuesHolder valuesHolder1=PropertyValuesHolder.ofFloat("scaleY",
                0.5f,1f);
        ObjectAnimator objectAnimator=ObjectAnimator.ofPropertyValuesHolder(view,valuesHolder,valuesHolder1);
        objectAnimator.setDuration(1000);
        objectAnimator.setInterpolator(new NewInterpolator());//自定义的插值器
        objectAnimator.start();

    }

    //如果登陆失败执行的方法
    private void reconvery(){
        //进度条开始隐藏
        progress.setVisibility(View.GONE);
        //显示登陆框布局
        inputlayout.setVisibility(View.VISIBLE);
        //显示账号密码框
        input_name.setVisibility(View.VISIBLE);
        input_pwd.setVisibility(View.VISIBLE);

        //设置属性存储器
        ViewGroup.MarginLayoutParams params= (ViewGroup.MarginLayoutParams) inputlayout.getLayoutParams();
        params.leftMargin=0;
        params.rightMargin=0;
        inputlayout.setLayoutParams(params);

        ObjectAnimator animator=ObjectAnimator.ofFloat(inputlayout,"scaleX",0,1f);
        animator.setDuration(1000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.start();
    //显示登陆按钮
        login.setVisibility(View.VISIBLE);

    }


/**
 * 设置自定义插值器
 * */
    public  class NewInterpolator extends LinearInterpolator{
             private  float factor;

        public NewInterpolator() {
            this.factor = 0.15f;
        }

        public float getInterpolator(float input){

            return (float) (Math.pow(2,-10*input)*Math.sin((input-factor/4)*(2*Math.PI)/factor)+1);
        }
    }
}
